<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="店铺ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <j-search-select-tag v-model="model.shopId" dict="ht_shop_store,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用户ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-search-select-tag v-model="model.userId" dict="sys_user,username,id" />
            </a-form-model-item>
          </a-col> -->
          <address-cascader ref="addressForm" />

          <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactPerson">
              <a-input v-model="model.contactPerson" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="手机电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phoneNumber">
              <a-input v-model="model.phoneNumber" placeholder="请输入手机电话"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="座机电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="telephone">
              <a-input v-model="model.telephone" placeholder="请输入座机电话"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入邮箱"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipcode">
              <a-input-number v-model="model.zipcode" placeholder="请输入邮编" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设置为默认地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isDefault">
              <a-checkbox v-model="model.isDefault">
                <!-- <label>设置为默认地址</label> -->
              </a-checkbox>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { mapActions, mapGetters, mapState, mapMutations } from 'vuex'

import AddressCascader from '@/components/htxincailiao/address/AddressCascader.vue'

export default {
  name: 'HTShopAddressForm',
  components: {
    AddressCascader
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      loading: true,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        // userId: [{ required: true, message: '请输入用户ID!' }],
        // provinceId: [{ required: true, message: '请输入省级ID!' }],
        // cityId: [{ required: true, message: '请输入市级ID!' }],
        // districtId: [{ required: true, message: '请输入区ID!' }],
        // streetAddress: [{ required: true, message: '请输入街道地址!' }],
        // suiteAddress: [{ required: true, message: '请输入楼/门牌号!' }],
        contactPerson: [{ required: true, message: '请输入联系人!' }],
        phoneNumber: [
          { required: true, message: '请输入手机电话!' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码' }
        ],
        email: [
          { required: false },
          { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的邮箱!' }
        ],
        zipcode: [{ required: false }, { pattern: /^[1-9]\d{5}$/, message: '请输入正确的邮政编码!' }]
      },
      url: {
        add: '/address/hTBuyerAddress/add',
        edit: '/address/hTBuyerAddress/edit',
        queryById: '/address/hTBuyerAddress/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },

  methods: {
    ...mapActions('address', ['getAddressListAction', 'saveAddressAction', 'deleteAddressAction', 'editAddressAction', 'setAddressAsDefaultAction']),

    add() {
      this.visible = true
    },

    edit(record) {
      this.model = Object.assign({}, record)
      this.$refs.addressForm.edit(record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          // let httpurl = ''
          // let method = ''
          // if (!this.model.id) {
          //   httpurl += this.url.add
          //   method = 'post'
          // } else {
          //   httpurl += this.url.edit
          //   method = 'put'
          // }

          const parameters = {
            ...this.model,
            ...this.$refs.addressForm.getAddressModel()
          }
          console.log('\n\n AddressModel  ' + JSON.stringify(this.$refs.addressForm.getAddressModel()) + '\n\n')
          console.log('\n\n parameters ' + JSON.stringify(parameters) + '\n\n')
          console.log('\n\n after this.model ' + JSON.stringify(this.model) + '\n\n')
          if (!that.model.id) {
            that
              .saveAddressAction(parameters)
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                }
              })
              .catch(res => {
                that.$message.error(res.message)
              })
              .finally(() => {
                that.confirmLoading = false
              })
          } else {
            that
              .editAddressAction(parameters)
              .then(res => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.error(res.message)
                }
              })
              .catch(res => {
                that.$message.error(res.message)
              })
              .finally(() => {
                that.confirmLoading = false
              })
          }

          // httpAction(httpurl, parameters, method)
          //   .then(res => {
          //     if (res.success) {
          //       that.$message.success(res.message)
          //       that.$emit('ok')
          //     } else {
          //       that.$message.warning(res.message)
          //     }
          //   })
          //   .finally(() => {
          //     that.confirmLoading = false
          //   })
        }
      })
    }
  }
}
</script>
